package com.wanshi.servlet;

import com.alibaba.fastjson.JSON;
import com.wanshi.entity.Result;
import com.wanshi.entity.User;
import com.wanshi.service.CartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/cart/addCartAjax")
public class addCartAjax extends HttpServlet {

    private CartService cartService = new CartService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1  获取 jsp传递过来的 prodid
        String prodid = request.getParameter("prodid");

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");


        //2 添加商品到购物车
        cartService.addCart(user.getId()+"",prodid);
        
        //3 跳转到购物车  servlet 界面
        response.getWriter().write(JSON.toJSONString(Result.ok()));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

